/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mathma.core.bool;

import mathma.core.expr.Expression;

import mathma.core.form.MultiExpression;

import java.util.List;

/**
 * Logical conjunction.  Returns true if and only if all operands are true.
 *
 * @since 0.2.4
 * @author Ethan Levine
 */
public class AndExpression
        extends MultiExpression<BooleanValue, BooleanValue> {

    /**
     * Creates a new AndExpression using a variable number of arguments.
     *
     * @param operands the operands of the conjunction.
     */
    public AndExpression(Expression<BooleanValue>... operands) {
        super(BooleanValue.Type, operands);
    }

    /**
     * Creates a new AndExpression using a list of arguments.
     *
     * @param operands the operands of the conjunction.
     */
    public AndExpression(List<Expression<BooleanValue>> operands) {
        super(BooleanValue.Type, operands);
    }

    /**
     * Evaluates this conjunction.  This returns true if and only if all of its
     * operands evaluate to true.  In other words, this returns false if and
     * only if at least one of its operands evaluates to false.
     *
     * @return the result of this conjunction.
     */
    public BooleanValue evaluate() {
        for (Expression<BooleanValue> operand : getOperands()) {
            if (!operand.evaluate().getRawValue()) {
                return BooleanValue.False;
            }
        }
        return BooleanValue.True;
    }

    public AndExpression newInstance(List<Expression<BooleanValue>> operands) {
        return new AndExpression(operands);
    }
}
